package com.bdc.whatwhat.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.actionbarsherlock.internal.nineoldandroids.animation.AnimatorSet;
import com.actionbarsherlock.internal.nineoldandroids.animation.FloatEvaluator;
import com.actionbarsherlock.internal.nineoldandroids.animation.ValueAnimator;

/**
 * Created by gfimac on 18/07/2014.
 */
public class ProgressCircleXp extends View {

    int color1 = Color.parseColor("#f2ff65");
    int color2 = Color.parseColor("#65ff86");
    float pourcentage = 0.8f;
    float p = 0;

    public ProgressCircleXp(Context context) {
        super(context);
    }

    public ProgressCircleXp(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressCircleXp(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setPourcentage(float pourcentage){
        this.pourcentage = pourcentage;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int mWidth = getWidth();
        int mHeight = getHeight();

        int centerX = mWidth / 2;
        int centerY = mHeight / 2;

        int mRadius = (int) ((mWidth / 2) * 0.95);

        final RectF rect = new RectF();
        rect.set(centerX - mRadius, centerY - mRadius, mWidth / 2 + mRadius, mHeight / 2 + mRadius);

        {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.parseColor("#aaFFFFFF"));

            canvas.drawCircle(centerX, centerY, mRadius, paint);
        }
        {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(1);
            paint.setStrokeCap(Paint.Cap.SQUARE);
            paint.setStyle(Paint.Style.FILL);

            if(!isInEditMode()) {
                int[] colors = {color1, color2};
                float[] positions = {0, 1};
                SweepGradient gradient = new SweepGradient(mWidth / 2, mHeight / 2, colors, positions);
                paint.setShader(gradient);
            }

            canvas.rotate(-90, centerX, centerY);

            if(isInEditMode())
                canvas.drawArc(rect, 0, 80*1/360, true, paint);
            else
                canvas.drawArc(rect, 0, p*360, true, paint);
        }

    }

    public void animer() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                ValueAnimator animator = ValueAnimator.ofObject(new ProgressEvaluator(), 0f, pourcentage);
                animator.setDuration(1500);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.start();
            }
        }, 500);

    }


    private class ProgressEvaluator extends FloatEvaluator {
        public ProgressEvaluator() {
        }

        @Override
        public Float evaluate(float fraction, Number startValue, Number endValue) {
            p = super.evaluate(fraction, startValue, endValue);

            invalidate();

            return p;
        }

    }
}
